﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BUS;
using System.Data.SqlClient;
namespace GPBenhLanh
{
    public partial class frmThongKe : DevExpress.XtraEditors.XtraForm
    {
        public frmThongKe()
        {
            InitializeComponent();
        }
        SqlConnection _conpk;

        public SqlConnection Conpk
        {
            get { return _conpk; }
            set { _conpk = value; }
        }
        private void btnThongKeNgay_Click(object sender, EventArgs e)
        {
            string tungay = datTuNgay.DateTime.ToString("yyyy-MM-dd");
            string denngay = datDenNgay.DateTime.ToString("yyyy-MM-dd");
            string dviGui = comDVLayMau.EditValue.ToString();
            if (chkDonVi.Checked == true)
                dviGui = "";
           
            if (chkTongHop.Checked == true)
            {
                dsXacNhan dsSoQLy = new dsXacNhan();
                DataTable SoCa = new DataTable();
                SoCa = ThongKeBUS.BC_ThuLao_THXN(tungay, denngay);
                SoCa.DefaultView.Sort = "TenDVu desc";
                SoCa = SoCa.DefaultView.ToTable();
               
                double TongTriGia = 0;
                if (SoCa != null)
                {
                    DataRow drAdd;
                    double DonGia = 0;
                    for (int i = 0; i < SoCa.Rows.Count; i++)
                    {
                        drAdd = dsSoQLy.dtBCThuLao_TH.NewRow();
                        drAdd["DVuCode"] = SoCa.Rows[i]["DVuCode"];
                        drAdd["TenDVu"] = SoCa.Rows[i]["TenDVu"];
                        drAdd["DonGia"] = SoCa.Rows[i]["DonGia"];
                        drAdd["SlgNTru"] = SoCa.Rows[i]["SlgNTru"];
                        drAdd["SlgNgTru"] = SoCa.Rows[i]["SlgNgTru"];
                        drAdd["SlgCTtrinh"] = SoCa.Rows[i]["SlgCTtrinh"];
                        drAdd["SlgKSK"] = SoCa.Rows[i]["SlgKSK"];
                        drAdd["SlgMG"] = SoCa.Rows[i]["SlgMG"];
                        drAdd["SlgTong"] = SoCa.Rows[i]["SlgTong"];

                        DonGia = Convert.ToDouble(SoCa.Rows[i]["DonGia"]);
                        TongTriGia += DonGia * Convert.ToDouble(SoCa.Rows[i]["SlgTong"]);

                        drAdd["TriGiaNTru"] = Convert.ToInt32(SoCa.Rows[i]["SlgNTru"]) * DonGia;
                        drAdd["TriGiaNgTru"] = Convert.ToInt32(SoCa.Rows[i]["SlgNgTru"]) * DonGia;
                        drAdd["TriGiaCT"] = Convert.ToInt32(SoCa.Rows[i]["SlgCTtrinh"]) * DonGia;
                        drAdd["TriGiaMG"] = Convert.ToInt32(SoCa.Rows[i]["SlgMG"]) * DonGia;
                        drAdd["TriGiaKSK"] = Convert.ToInt32(SoCa.Rows[i]["SlgKSK"]) * DonGia;

                        dsSoQLy.dtBCThuLao_TH.Rows.Add(drAdd);
                    }

                    rptBCThuLao_THXN rpt = new rptBCThuLao_THXN();
                    DateTime date = DateTime.Parse(datTuNgay.EditValue.ToString());
                    DateTime date2 = DateTime.Parse(datDenNgay.EditValue.ToString());
                    if (date == date2)
                    {
                        rpt.txtNgayBaoCao.Text = "Ngày " + date.Day.ToString("00")
                                 + " Tháng " + date.Month.ToString("00")
                                 + " Năm " + date.Year;
                    }
                    else
                    {
                        rpt.txtNgayBaoCao.Text = "Từ Ngày " + date.Day.ToString("00")
                                + "/" + date.Month.ToString("00")
                                + "/" + date.Year
                                + " Đến Ngày " + date2.Day.ToString("00")
                                + "/" + date2.Month.ToString("00")
                                + "/" + date2.Year;
                    }

                    rpt.txtNgayBC.Text = "Ngày " + DateTime.Now.Day.ToString("00")
                                   + " tháng " + DateTime.Now.Month.ToString("00")
                                   + " năm " + DateTime.Now.Year;

                    rpt.txtTongGiaTri.Text = TongTriGia.ToString();
                    //rpt.lblDonVi.Text = "Phòng: " + User.TenDonVi;
                    rpt.lblGioIn.Text = DateTime.Now.ToString();
                    rpt.DataSource = dsSoQLy;
                    rpt.ShowPreview();
                }               
            }
            else
            {
                DataTable dt = ThongKeBUS.ThongKeDaXN(tungay, denngay, dviGui);
                rptDaXacNhan rpt = new rptDaXacNhan();
                dsXacNhan ds = new dsXacNhan();
                DataRow dr;
                if (dt == null)
                    return;
                //do data vao dataset
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dr = ds.dsDaXacNhan.NewRow();
                    dr["SoHS"] = dt.Rows[i]["SoHS"].ToString().ToUpper();
                    dr["SoBN"] = dt.Rows[i]["SoBN"].ToString();
                    dr["SoXanh"] = dt.Rows[i]["SoXanh"].ToString();
                    dr["NamSinh"] = dt.Rows[i]["NamSinh"].ToString()
                                    + " (" + dt.Rows[i]["tuoi"].ToString() + ")";
                    dr["tuoi"] = dt.Rows[i]["tuoi"].ToString();
                    dr["HoTen"] = dt.Rows[i]["HoTen"].ToString();
                    dr["Ten"] = dt.Rows[i]["Ten"].ToString();
                    dr["SoTien"] = dt.Rows[i]["SoTien"].ToString();

                    dr["donvicd"] = dt.Rows[i]["donvicd"].ToString();
                    dr["dvGuiMau"] = dt.Rows[i]["dvGuiMau"].ToString();
                    dr["maDVGM"] = dt.Rows[i]["maDVGM"].ToString();
                    dr["MaGPB"] = dt.Rows[i]["MaGPB"].ToString();
                    dr["GhiChu"] = dt.Rows[i]["GhiChu"].ToString();
                    dr["mau"] = dt.Rows[i]["mau"].ToString();
                    dr["idmau"] = dt.Rows[i]["idmau"].ToString();

                    dr["tongTB"] = dt.Rows[i]["tongTB"].ToString();
                    dr["tongGPB"] = dt.Rows[i]["tongGPB"].ToString();
                    dr["SoTieuBan"] = dt.Rows[i]["SoTieuBan"].ToString();
                    dr["stt"] = dt.Rows[i]["stt"].ToString();
                    dr["NgayGui"] = DateTime.Parse(dt.Rows[i]["CDate"].ToString()).ToString("dd-MM");
                    dr["DVuCode"] = dt.Rows[i]["DVuCode"].ToString();
                    ds.dsDaXacNhan.Rows.Add(dr);
                }
                rpt.DataSource = ds;
                rpt.ShowPreviewDialog();
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmThongKe_Load(object sender, EventArgs e)
        {
            datTuNgay.DateTime = DateTime.Now.Date;
            datDenNgay.DateTime = DateTime.Now.Date;
            comDVLayMau.Properties.DataSource = GetThongTinSearchBUS.SearchDonViGuiMau();
            comDVLayMau.Properties.DisplayMember = "TenDonVi";
            comDVLayMau.Properties.ValueMember = "MaDonVi";
        }

        private void chkDonVi_CheckedChanged(object sender, EventArgs e)
        {
            if (chkDonVi.Checked == true)
                comDVLayMau.Enabled = false;
            else
                comDVLayMau.Enabled = true;
        }
    }
}